
#include "../include/cae.h"

int wk_count = 0;

/* ------------------------------------------------------------------------
** Namespace
** ------------------------------------------------------------------------ */
using namespace std;
/* ------------------------------------------------------------------------
** Function
** ------------------------------------------------------------------------ */


int CAEAudioCb(CAE_HANDLE handle, const char *audiodata, unsigned int audiolen, void *userdata, void *reserved)
{
	CAEUserData *usDta = (CAEUserData*)userdata;
	fwrite(audiodata, 1, audiolen, usDta->fp_out_VAD);
	
	return 0;
}

int CAEIvwAudioCb(CAE_HANDLE handle, const char *audiodata, unsigned int audiolen, void *userdata, void *reserved)
{
	return 0;
}

int CAEIvwCb(CAE_HANDLE handle, short angle, short beam, char* keyword, float power, short score, void *userdata, void *reserved)
{	
    wk_count++;

	printf("CAEIvwCb | ---Wkup No:%02d --- beam:%d, angle:%d, score:%d---\n", wk_count, beam, angle, score);
	return 0;
}

//char mMem[380000];
char mMem[480000];

int main( int argc, char *argv[] )
{
	int i;
	int ret;

    char *pcmBuf = NULL;
	char* pcmBuf2 = NULL;
    char *f_pcmBuf = NULL;
	char* f_pcmBuf2 = NULL;

	char* refBuf = NULL;
	char* f_refBuf = NULL;

	char* refBuf2 = NULL;
	char* f_refBuf2 = NULL;

	char *version = NULL;
    unsigned long pcmSize = 0;
	int once_size = 512 * sizeof(short);
    unsigned long totalsize = 0;

    const char *resPath_old = "ivw_70_3004.bin";

	CAEUserData userData;
	unsigned long nOutFrame = 0;
	int beam;
	char snr_val[20];
	unsigned int len;
	float aec_gain = 8.0f;
	char* ncm = "0:730";
	char ncm_out[64];
	short noneref[512];

	finput_mic1 = fopen(INPUT_FILE_MIC1, "rb");
	finput_mic2 = fopen(INPUT_FILE_MIC2, "rb");
	finput_ref1 = fopen(INPUT_FILE_REF1, "rb");
	finput_ref2 = fopen(INPUT_FILE_REF2, "rb");
    if (NULL == finput_mic1 || NULL == finput_mic2 || NULL == finput_ref1 || NULL == finput_ref2){
	    printf("fopen micphone or reference failed\n");
	    return -1;
    }
    fseek(finput_mic1, 0, SEEK_END);
    pcmSize = ftell(finput_mic1);
    totalsize = pcmSize;
    pcmBuf = (char*)malloc(once_size);
	pcmBuf2 = (char*)malloc(once_size);
	refBuf = (char*)malloc(once_size);
	refBuf2 = (char*)malloc(once_size);

    if (NULL == pcmBuf || NULL == pcmBuf2 || NULL == refBuf || NULL == refBuf2){
	    printf("malloc mic buffer or ref buffer failed\n");
	    return -1 ;
    }
	f_pcmBuf = pcmBuf;//for free
	f_pcmBuf2 = pcmBuf2;
	f_refBuf = refBuf;
	f_refBuf2 = refBuf2;
    memset(pcmBuf, 0, once_size);
	memset(pcmBuf2, 0, once_size);
	memset(refBuf, 0, once_size);
	memset(refBuf2, 0, once_size);

    fseek(finput_mic1, 0, SEEK_SET);
	
	
	userData.flag = 0;
	userData.fp_out_VAD = fopen("intf_vad_out.pcm", "wb");
    if (NULL == userData.fp_out_VAD){
	    return -1;
    }

	ret = CAENew(&cae, mMem, NULL);
	CAESetListener(cae, 0, (void*)CAEIvwCb, &userData);
	CAESetListener(cae, 1, (void*)CAEAudioCb, &userData);

	/* ------------------------------------------ */
	/*                CAESetParam                 */
	/* ------------------------------------------ */
	beam = 1;
	len = 0;
	CAESetParam(cae, 7, ncm, strlen(ncm));
	len = 64;
	CAEGetParam(cae, 7, ncm_out, &len);
	////printf("%s\n", ncm_out);
	version = CAEGetVersion();
	/* ------------------------------------------ */
	/*               CAEAudioWrite                */
	/* ------------------------------------------ */

	for (int k = 0; k < 512; k++)
	{
		noneref[k] = 0;
	}


	for ( int i = 0;i < (totalsize/once_size);i++ )
	{
		fread(pcmBuf, 1, once_size, finput_mic1);
		fread(pcmBuf2, 1, once_size, finput_mic2);
		fread(refBuf, 1, once_size, finput_ref1);
		fread(refBuf2, 1, once_size, finput_ref2);

		printf("Processing: %.2f%%\r", i*100.0f/(totalsize/once_size));
		CAEAudioWrite(cae, pcmBuf, once_size);
		//CAEAudioWrite(cae, pcmBuf, refBuf, refBuf2 /*(const char*)noneref, (const char*)noneref*/, once_size);
		nOutFrame++;
	}

	/* ------------------------------------------ */
	/*                  Destroy                   */
	/* ------------------------------------------ */
	CAEDestroy(cae);
	free(f_pcmBuf);
	free(f_refBuf);
	fclose(userData.fp_out_VAD);
	fclose(finput_mic1);
	fclose(finput_mic2);
	system("pause");

	return 0;
}

